#include "process.h"
#include <string.h>
#include <unistd.h>
#define SIZE 101 //给\0预留一个位置
#define STYLE '#' //给进度条设置一个字符



//V2

void FlushProcess(double total, double current)
{
    const char * lable ="|/-\\"; 
    int len = strlen(lable);
    static int index = 0;

    char buffer[SIZE];
    memset(buffer, 0, sizeof(buffer));

    double rate = current*100.0/total;
    int num = (int)rate;
    
    int i = 0;
    for(; i < num; i++)
    {
        buffer[i] = STYLE;


    }
    printf("[%-100s][%.1lf%%][%c]\r", buffer, rate, lable[index++]);
    fflush(stdout);
    
    index %= len;
    
    //if(num >= 100) printf("\n");
}





//V1：实现进度条基本功能
void process()
{
    int rate = 0; //创建rate变量表示进度条的比率
    //进度条的进度假设用100个字符显示，创建一个数组能存放100个字符
    char buffer[SIZE];
    memset(buffer, 0, sizeof(buffer));
    const char * lable = "|/-\\";
    int len = strlen(lable);
    while(rate <= 100)
    {
        printf("[%-100s][%d%%][%c]\r" ,buffer, rate, lable[rate%len]); //不要用\n，要用\r将光标反复移回行首
        fflush(stdout); //由于是行刷新策略，所以要强制刷新，否则不显示
        buffer[rate] = STYLE;
        usleep(50000);
        rate++;
    }
    printf("\n");
}

